package com.example.pclimb.dao;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.pclimb.model.User;

import java.util.List;

/**
 * 用户数据访问接口
 */
@Dao
public interface UserDao {
    @Insert
    long insert(User user);

    @Update
    void update(User user);

    @Delete
    void delete(User user);

    @Query("SELECT * FROM users WHERE id = :id")//避免了 SQL 注入攻击，因为 Room 会自动处理参数的转义和安全问题。
    LiveData<User> getUserById(int id);

    @Query("SELECT * FROM users WHERE username = :username")
    LiveData<User> getUserByUsername(String username);

    @Query("SELECT * FROM users WHERE username = :username AND password = :password")
    LiveData<User> login(String username, String password);

    @Query("SELECT * FROM users WHERE userType = 2")
    LiveData<List<User>> getAllApprovedGuides();

    @Query("SELECT * FROM users WHERE isGuideApplied = 1 AND isGuideApproved = 0")
    LiveData<List<User>> getPendingGuideApplications();
    
    @Query("SELECT * FROM users WHERE guideApplication = :applicationStatus")
    LiveData<List<User>> getUsersByApplicationStatus(int applicationStatus);

    @Query("UPDATE users SET isGuideApproved = 1, userType = 2 WHERE id = :userId")
    void approveGuideApplication(int userId);

    @Query("UPDATE users SET isGuideApproved = 0 WHERE id = :userId")
    void rejectGuideApplication(int userId);

    @Query("UPDATE users SET rating = :newRating, ratingCount = ratingCount + 1 WHERE id = :userId")
    void updateRating(int userId, float newRating);
} 